﻿namespace Licens
{
    using DevExpress.Utils;
    using DevExpress.XtraEditors;
    using Licens.Class;
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;

    public class frmactivekey : Form
    {
        private bool _bok = false;
        private SimpleButton btnok;
        private IContainer components = null;
        private Label label1;
        private Label label2;
        private Label label3;
        private Label lblngay;
        private TextEdit txtkey;
        private TextEdit txtmaso1;
        private TextEdit txtmaso2;
        private TextEdit txtmaso3;
        Access _acc;
        int _idChuongTrinh = 1;
        /// <summary>
        /// ID chương trình đăng ký sử dụng.
        /// </summary>
        public int pIDChuongTrinh
        {
            set { _idChuongTrinh = value; }
            get { return _idChuongTrinh; }
        }
        public frmactivekey()
        {
            this.InitializeComponent();
        }

        public void btnok_Click(object sender, EventArgs e)
        {
            this._bok = false;
            
            if (_acc.f_load_madangky() == "")
            {
                //string vmaso = this.txtmaso1.Text + txtmaso2.Text + this.txtmaso3.Text;
                string key = _acc.f_get_capkey( 30, DateTime.Now.ToString("dd/MM/yyyy"), false);
                _acc.f_save_madangky(key);
                this.txtkey.Text = key;
                this.lblngay.Text = "30 ngày.";
                this._bok = true;
            }
            else
            {
                string text = this.txtkey.Text;
                Class.Keys vkey = new Class.Keys(text);

                if (!_acc.f_kiemtra_madangky(vkey))
                {
                    MessageBox.Show("Licenses không hợp lệ.", "Thông báo");
                    this._bok = false;
                }
                else if (!_acc.f_kiemtra_thoihan(vkey))
                {
                    MessageBox.Show("Licenses đã hết thời hạn sử dụng.", "Thông báo");
                    this._bok = false;
                }
                else
                {
                    int num = vkey.pSoNgaySuDung;
                    if (num == 999)
                    {
                        this.lblngay.Text = "Không thời hạn";
                    }
                    else
                    {
                        this.lblngay.Text = vkey.pSoNgaySuDung.ToString()+" ngày.";
                    }
                    this._bok = true;
                    _acc.f_save_madangky(text);
                }
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        public bool f_kiemtra_dangkylicenses()
        {
            this.frmactivekey_Load(null, null);
            if (this.txtkey.Text == "")
            {
                MessageBox.Show("Chưa đăng ký sử dụng phần mềm.", "Thông báo");
                return false;
            }
            this.btnok_Click(null, null);
            return this._bok;
        }

        public void frmactivekey_Load(object sender, EventArgs e)
        {
            _acc = new Access(_idChuongTrinh);
            string key = _acc.f_load_madangky();
            string str = key;
            
            Class.Keys vkey = new Class.Keys(str);
            vkey.pIDChuongTrinh = _idChuongTrinh;
            str = vkey.f_CreateCodeNumber();

            this.txtmaso1.Text = str.Substring(0, 6);
            this.txtmaso2.Text = str.Substring(6, 6);
            this.txtmaso3.Text = str.Substring(12);

            
            try
            {
                if (key != "")
                {
                    double num =Convert.ToDouble( vkey.pSoNgaySuDung);
                    string vdate = vkey.pNgayBatDauSuDung;
                    DateTime time = new DateTime(int.Parse(vdate.Substring(6, 4)), int.Parse(vdate.Substring(3, 2)), int.Parse(vdate.Substring(0, 2)));
                    time=time.AddDays(num);
                    if (num != 999)
                    {
                        this.lblngay.Text = time.ToString("dd/MM/yyyy");
                    }
                    else
                    {
                        this.lblngay.Text = "Không thời hạn";
                    }
                    this.txtkey.Text = key;
                }
                else
                {
                    this.btnok_Click(null, null);
                }
            }
            catch
            {
            }
        }

        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmactivekey));
            this.txtmaso1 = new DevExpress.XtraEditors.TextEdit();
            this.label1 = new System.Windows.Forms.Label();
            this.txtmaso2 = new DevExpress.XtraEditors.TextEdit();
            this.txtmaso3 = new DevExpress.XtraEditors.TextEdit();
            this.label2 = new System.Windows.Forms.Label();
            this.txtkey = new DevExpress.XtraEditors.TextEdit();
            this.btnok = new DevExpress.XtraEditors.SimpleButton();
            this.label3 = new System.Windows.Forms.Label();
            this.lblngay = new System.Windows.Forms.Label();
            ((System.ComponentModel.ISupportInitialize)(this.txtmaso1.Properties)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.txtmaso2.Properties)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.txtmaso3.Properties)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.txtkey.Properties)).BeginInit();
            this.SuspendLayout();
            // 
            // txtmaso1
            // 
            this.txtmaso1.Location = new System.Drawing.Point(59, 11);
            this.txtmaso1.Name = "txtmaso1";
            this.txtmaso1.Properties.ReadOnly = true;
            this.txtmaso1.Size = new System.Drawing.Size(67, 20);
            this.txtmaso1.TabIndex = 5;
            // 
            // label1
            // 
            this.label1.Font = new System.Drawing.Font("Tahoma", 10F);
            this.label1.Location = new System.Drawing.Point(2, 9);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(60, 23);
            this.label1.TabIndex = 4;
            this.label1.Text = "Mã số:";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // txtmaso2
            // 
            this.txtmaso2.Location = new System.Drawing.Point(132, 11);
            this.txtmaso2.Name = "txtmaso2";
            this.txtmaso2.Properties.ReadOnly = true;
            this.txtmaso2.Size = new System.Drawing.Size(67, 20);
            this.txtmaso2.TabIndex = 5;
            // 
            // txtmaso3
            // 
            this.txtmaso3.Location = new System.Drawing.Point(205, 11);
            this.txtmaso3.Name = "txtmaso3";
            this.txtmaso3.Properties.ReadOnly = true;
            this.txtmaso3.Size = new System.Drawing.Size(67, 20);
            this.txtmaso3.TabIndex = 5;
            // 
            // label2
            // 
            this.label2.Font = new System.Drawing.Font("Tahoma", 10F);
            this.label2.Location = new System.Drawing.Point(2, 34);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(60, 23);
            this.label2.TabIndex = 4;
            this.label2.Text = "Keys:";
            this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // txtkey
            // 
            this.txtkey.Location = new System.Drawing.Point(59, 37);
            this.txtkey.Name = "txtkey";
            this.txtkey.Size = new System.Drawing.Size(213, 20);
            this.txtkey.TabIndex = 5;
            // 
            // btnok
            // 
            this.btnok.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
            this.btnok.Appearance.Options.UseTextOptions = true;
            this.btnok.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Near;
            this.btnok.Image = ((System.Drawing.Image)(resources.GetObject("btnok.Image")));
            this.btnok.Location = new System.Drawing.Point(124, 85);
            this.btnok.Name = "btnok";
            this.btnok.Size = new System.Drawing.Size(75, 23);
            this.btnok.TabIndex = 39;
            this.btnok.Text = " &Acvtive";
            this.btnok.Click += new System.EventHandler(this.btnok_Click);
            // 
            // label3
            // 
            this.label3.Font = new System.Drawing.Font("Tahoma", 10F);
            this.label3.Location = new System.Drawing.Point(2, 60);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(98, 23);
            this.label3.TabIndex = 4;
            this.label3.Text = "Sử dụng đến:";
            this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // lblngay
            // 
            this.lblngay.Font = new System.Drawing.Font("Tahoma", 10F);
            this.lblngay.Location = new System.Drawing.Point(93, 59);
            this.lblngay.Name = "lblngay";
            this.lblngay.Size = new System.Drawing.Size(171, 23);
            this.lblngay.TabIndex = 4;
            this.lblngay.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // frmactivekey
            // 
            this.ClientSize = new System.Drawing.Size(290, 115);
            this.Controls.Add(this.btnok);
            this.Controls.Add(this.txtmaso3);
            this.Controls.Add(this.txtmaso2);
            this.Controls.Add(this.txtkey);
            this.Controls.Add(this.txtmaso1);
            this.Controls.Add(this.lblngay);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "frmactivekey";
            this.ShowIcon = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Mã đăng ký sử dụng";
            this.Load += new System.EventHandler(this.frmactivekey_Load);
            ((System.ComponentModel.ISupportInitialize)(this.txtmaso1.Properties)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.txtmaso2.Properties)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.txtmaso3.Properties)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.txtkey.Properties)).EndInit();
            this.ResumeLayout(false);

        }

        public bool pActiveSuccess
        {
            get
            {
                return this._bok;
            }
        }
    }
}

